/*
 * @(#)jTunes.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.client.player;

import javax.swing.JFrame;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.netexplode.jtunes.client.ui.component.MainPanel;
import com.netexplode.jtunes.model.media.CollectionManager;
import com.netexplode.jtunes.model.media.Tag;

/**
 * <code>jTunes</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.8 $
 * @since 0.1
 */
public class jTunes {

	private static final Log log = LogFactory.getLog(jTunes.class);

	private Initializer initializer;

	private static MainPanel mainPanel;

	private static CollectionManager collectionManager;

	public void setInitializer(Initializer initializer) {
		this.initializer = initializer;
	}

	public void setCollectionManager(CollectionManager collectionManager) {
		this.collectionManager = collectionManager;
	}

	public void setMainPanel(MainPanel mainPanel) {
		this.mainPanel = mainPanel;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		log.info("Starting up " + jTunes.class.getSimpleName());

		final ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] {
				"applicationContext.xml", "componentContext.xml" });

		jTunes app = (jTunes) applicationContext.getBean(jTunes.class.getName());
		app.init();

		log.info("Exiting " + jTunes.class.getSimpleName());
		
		// System.exit(0);
	}

	private void init() {
		// this.initializer.init();

		if (!mainPanel.isInitialized()) {
			mainPanel.init();
		}

		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}

	private void createAndShowGUI() {
		// Make sure we have nice window decorations.
		JFrame.setDefaultLookAndFeelDecorated(true);

		// Create and set up the window.
		JFrame frame = new JFrame("jTunes");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(mainPanel);
		// frame.setSize(640, 480);
		frame.setResizable(true);

		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public synchronized static void refreshLibraryPanel() {
		mainPanel.refreshLibraryPanel();
	}

	public synchronized static void refreshTree() {
		mainPanel.refreshTree();
	}

	public static void refreshSharedCollectionPanel() {
		new Thread() {
			public void run() {
				mainPanel.refreshSharedCollectionPanel();
				refreshLibraryPanel();
				refreshTree();
			}
		}.start();
		// main.refreshSharedCollectionPanel();
		// refreshLibraryPanel();
		// refreshTree();
	}

	public synchronized static void setAlbumArtPanelVisible(boolean visible) {
		mainPanel.setAlbumArtPanelVisible(visible);
	}

	public synchronized static void setLcdPanelText() {
		// @todo update displayable tag string
		final Tag tag = collectionManager.getCurrentMediaFile().getTag();
		mainPanel.setLcdPanelText(tag.getTitle() + "\n" + " - \n" + tag.getArtist());
	}
}